package com.jichangxiu.common.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.github.linpeilie.Converter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ConvertUtils {

    private final static Converter CONVERTER = SpringUtil.getBean(Converter.class);

    public static <T, V> V convert(T source, Class<V> desc) {
        if (ObjectUtil.isNull(source)) return null;
        if (ObjectUtil.isNull(desc)) return null;
        return CONVERTER.convert(source, desc);
    }

    public static <T, V> V convert(T source, V desc) {
        if (ObjectUtil.isNull(source)) return null;
        if (ObjectUtil.isNull(desc)) return null;
        return CONVERTER.convert(source, desc);
    }

    public static <T, V> List<V> convert(Collection<T> sourceList, Class<V> desc) {
        if (ObjectUtil.isNull(sourceList))
            return CollUtil.newArrayList();
        if (CollUtil.isEmpty(sourceList))
            return CollUtil.newArrayList();
        return CONVERTER.convert(ListUtil.toList(sourceList), desc);
    }

    public static <T, V> List<V> convert(List<T> sourceList, Class<V> desc) {
        if (ObjectUtil.isNull(sourceList))
            return CollUtil.newArrayList();
        if (CollUtil.isEmpty(sourceList))
            return CollUtil.newArrayList();
        return CONVERTER.convert(sourceList, desc);
    }

    public static <T> T convert(Map<String, Object> map, Class<T> beanClass) {
        if (MapUtil.isEmpty(map)) return null;
        if (ObjectUtil.isNull(beanClass)) return null;
        return CONVERTER.convert(map, beanClass);
    }

}
